package com.sgb.kjwl.view.ui.adapter.enterprise;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.enterprise.EpQuoteEntity;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.recyclerview.entity.BaseAreaEntity;
import com.swgk.core.util.DateUtils;
import com.swgk.core.util.PointUtils;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class EpQuoteAdapter extends BaseRecycleAdapter<EpQuoteEntity> {

    private String subStr;
    private boolean isHome;

    public EpQuoteAdapter(Context context, List<EpQuoteEntity> datas, boolean isHome) {
        super(context, R.layout.item_enterprise_zhuanyebaojia, datas);
        this.isHome = isHome;
    }

    @Override
    public void convert(ItemViewHolder holder, EpQuoteEntity epQuoteEntity) {
        holder.setBinding(BR.viewModel, epQuoteEntity);
        TextView tv_time_quote = holder.getItem().findViewById(R.id.tv_time);
        TextView tv_category = holder.getItem().findViewById(R.id.tv_category);
        TextView btn_chat = holder.getItem().findViewById(R.id.btn_chat);
        TextView price = holder.getItem().findViewById(R.id.price_quote);
        //临时调加区分是否大首页使用true 隐藏  false 显示
        if (isHome){
            btn_chat.setVisibility(View.GONE);
        }else {
            btn_chat.setVisibility(View.VISIBLE);
        }

        if(!TextUtils.isEmpty(epQuoteEntity.getDomesticCompaniesPrice())&&!TextUtils.isEmpty(epQuoteEntity.getMaximumPrice())){
            if(epQuoteEntity.getDomesticCompaniesPrice().equals("-1")) {
                price.setText("议价");
            }else{
                price.setText("¥"+PointUtils.price(Double.parseDouble(epQuoteEntity.getDomesticCompaniesPrice()))+"起");
            }
        }else{
            price.setText(epQuoteEntity.getPrice()!=-1?TextViewBindingAdapter.setMoneyForRMB("¥"+PointUtils.price(epQuoteEntity.getPrice())):"议价");
        }

        TextView tv_type1 = holder.getItem().findViewById(R.id.tv_type1);
        btn_chat.setOnClickListener(v -> iCallPhone.call(epQuoteEntity.getContactPhone(), epQuoteEntity.getContact()));
        tv_time_quote.setText(DateUtils.timestampTo13Date3(epQuoteEntity.getUpdateTime()));
        TextView serviceCities = holder.getItem().findViewById(R.id.tv_serviceCities);
        if (epQuoteEntity.getFirstCategoryName() != null && !epQuoteEntity.getFirstCategoryName().equals("")) {
            subStr = epQuoteEntity.getFirstCategoryName();
            if (epQuoteEntity.getSecondCategoryName() != null && !epQuoteEntity.getSecondCategoryName().equals("")) {
                String two = epQuoteEntity.getSecondCategoryName();
                subStr = subStr + "-" + two;
            }
            if (epQuoteEntity.getItemName() != null && !epQuoteEntity.getItemName().equals("")) {
                String three = epQuoteEntity.getItemName();
                subStr = subStr + "-" + three;
            }
        }
        tv_type1.setVisibility(View.VISIBLE);
        switch (epQuoteEntity.getPublishType()) {

            case 0:
                tv_type1.setText("个人");
                tv_type1.setBackgroundResource(R.drawable.bg_card_type_authen);
                break;
            case 1:
                tv_type1.setText("企业");
                tv_type1.setBackgroundResource(R.drawable.bg_card_type_authen);
                break;
            case 2:
                tv_type1.setText("团队");
                tv_type1.setBackgroundResource(R.drawable.bg_team_authen);
                break;
            default:
                tv_type1.setVisibility(View.GONE);
                break;
        }
        tv_category.setText(subStr);
        String cities = epQuoteEntity.getServiceCitys();
        ArrayList<BaseAreaEntity> citysList = jsonToArrayList(cities, BaseAreaEntity.class);
        StringBuilder stringBuilder = new StringBuilder();
        for (BaseAreaEntity entity : citysList) {
            stringBuilder.append(entity.getName()).append("/");
        }
        String cityShow = stringBuilder.toString().substring(0, stringBuilder.toString().length() - 1);
        serviceCities.setText(cityShow);
    }

    /**
     * @param json
     * @param clazz
     * @return
     */
    public static <T> ArrayList<T> jsonToArrayList(String json, Class<T> clazz) {
        Type type = new TypeToken<ArrayList<JsonObject>>() {
        }.getType();
        ArrayList<T> arrayList = new ArrayList<>();
        try {
            ArrayList<JsonObject> jsonObjects = new Gson().fromJson(json, type);

            for (JsonObject jsonObject : jsonObjects) {
                arrayList.add(new Gson().fromJson(jsonObject, clazz));
            }
        } catch (Exception e) {

        }

        return arrayList;
    }

    private ICallPhone iCallPhone;

    public interface ICallPhone {
        void call(String num, String name);
    }

    public void OnCallPhone(ICallPhone iCallPhone) {
        this.iCallPhone = iCallPhone;
    }
}
